#include "Stack.h"


void STInit(ST* head)
{
	STDataType* pf = (STDataType*)malloc(sizeof(STDataType) * 5);
	if (pf == NULL)
	{
		perror("malloc fail");
		return;
	}
	head->a = pf;
	head->top = 0;
	head->capacity = 5;
}

void STDestroy(ST* head)
{
	assert(head);
	free(head->a);
	head->a = NULL;
	head->capacity = 0;
	head->top = 0;
}

void STPush(ST* head, STDataType x)
{
	if (head->top == head->capacity)
	{
		STDataType* newnode = (STDataType*)realloc(head->a, sizeof(STDataType) * (head->capacity) * 2);
		if (newnode == NULL)
		{
			perror("realloc fail");
			return;
		}
		head->a = newnode;
		head->capacity *= 2;
	}
	head->a[head->top] = x;
	head->top++;
}

void STPop(ST* head)
{
	assert(head);
	assert(!STEmpty(head));
	head->top--;
}

STDataType STTop(ST* head)
{
	assert(head);
	assert(!STEmpty(head));
	return head->a[head->top - 1];
}

bool STEmpty(ST* head)
{
	return head->a == NULL;
}

int STSize(ST* head)
{
	return head->top;
}
